#include "anselmu.h"

Racket racket1;
Racket racket2;
Ball   ball;
Goal   goal1;
Goal   goal2;
Limits limits;

void initAnselmu()
{

    limits.x = MARGIN;
    limits.y = MARGIN;
    limits.w = SCREEN_W - (2 * MARGIN);
    limits.h = SCREEN_H - (2 * MARGIN);

    goal1.x = 0;
    goal1.y = (SCREEN_H / 2) - GOAL_HEIGHT /2;
    goal1.w = GOAL_WIDTH;
    goal1.h = GOAL_HEIGHT;

    goal2.x = SCREEN_W - GOAL_WIDTH;
    goal2.y = (SCREEN_H / 2) - GOAL_HEIGHT /2;
    goal2.w = GOAL_WIDTH;
    goal2.h = GOAL_HEIGHT;

    racket1.x = GOAL_WIDTH + RACKET_DISTANCE;
    racket1.y = (SCREEN_H / 2) - RACKET_HEIGHT /2;
    racket1.w = RACKET_WIDTH;
    racket1.h = RACKET_HEIGHT;

    racket2.x = SCREEN_W - (GOAL_WIDTH + RACKET_WIDTH + RACKET_DISTANCE);
    racket2.y = (SCREEN_H / 2) - RACKET_HEIGHT /2;
    racket2.w = RACKET_WIDTH;
    racket2.h = RACKET_HEIGHT;

    resetBall(&ball);
    ball.radius = BALL_RADIUS;
    ball.speed = 1.0f;

}


void drawRacket(Racket racket, unsigned int color)
{
    drawRect(racket.x, racket.y, racket.w, racket.h, color);
}

void drawGoal(Goal goal, unsigned int color)
{
    drawRect(goal.x, goal.y, goal.w, goal.h, color);
}
void drawBall(unsigned int color)
{
    drawRect(ball.x, ball.y, ball.radius, ball.radius, color);
}
void drawLimits(unsigned int color)
{
    drawLine(limits.x, limits.y, limits.x + limits.w, limits.y, color);
    drawLine(limits.x + limits.w, limits.y + limits.h, limits.x + limits.w, limits.y, color);
    drawLine(limits.x, limits.y + limits.h, limits.x + limits.w, limits.y + limits.h, color);
    drawLine(limits.x, limits.y + limits.h, limits.x, limits.y, color);
}

void drawAnselmu()
{
    drawLimits(WHITE);

    drawRacket(racket1, RED);
    drawRacket(racket2, BLUE);

    drawGoal(goal1, HOTPINK);
    drawGoal(goal2, HOTPINK);

    drawBall(GREEN);

    SDL_Surface *font = TTF_RenderText_Solid(oneDirection, "OLA K ASE ANSELMU", (SDL_Color){255,255,0});
    SDL_Rect rect = {SCREEN_W/2-120, 10,0,0};
    //Blit text_surface surface to the screen surface
    SDL_BlitSurface(font, NULL, screen, &rect);
    //Free the text_surface surface
    SDL_FreeSurface(font);
}

void resetBall(Ball *ball)
{
    ball->x = SCREEN_W/2;
    ball->y = SCREEN_H/2;
    randomizeBall(ball);
    Mix_PlayChannel(-1, ball_respawn_snd, 0);
}

void randomizeBall(Ball *ball)
{
    ball->angle = (float)DEG_TO_RAD(randNum(0, 360));
    calcBallInc(ball);
}

void calcBallInc(Ball *ball)
{
    ball->inc_x = cos(ball->angle);
    ball->inc_y = sin(ball->angle);
}

void moveBall(Ball *ball)
{
    ball->x += ball->inc_x * ball->speed;
    ball->y += ball->inc_y * ball->speed;
}

void moveAnselmu()
{
    moveBall(&ball);
    //check GOALS

    ballLimitsCollision(&ball);
}

void moveRacket(Racket *racket, int distance)
{
    racket->y += distance;
}

void ballLimitsCollision(Ball *ball)
{
    if((ball->x + ball->radius) > (limits.x + limits.w) )
    {
        ball->inc_x *= -1;
        ball->x = (limits.x + limits.w) - ball->radius;
        Mix_PlayChannel(-1, ball_hit_snd, 0);
    }

    if(ball->x < limits.x)
    {
        ball->inc_x *= -1;
        ball->x = limits.x;
        Mix_PlayChannel(-1, ball_hit_snd, 0);
    }

    if((ball->y + ball->radius) > (limits.y + limits.h) )
    {
        ball->inc_y *= -1;
        ball->y = (limits.y + limits.h) - ball->radius;
        Mix_PlayChannel(-1, ball_hit_snd, 0);
    }

    if(ball->y < limits.y)
    {
        ball->inc_y *= -1;
        ball->y = limits.y;
        Mix_PlayChannel(-1, ball_hit_snd, 0);
    }
}

